{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 开启通义千问大模型"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "cc8821920abf9915"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "import langchain\n",
    "from langchain_community.llms import Tongyi\n",
    "import os\n",
    "\n",
    "os.environ[\"DASHSCOPE_API_KEY\"] = \"sk-edf4bccb72d14fbabcb7f1d9369c8a65\"\n",
    "llm = Tongyi()\n",
    "llm.predict(\"hello\")"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "54c1f1f3267be427"
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 提示词模板- 字符串模板 PromptTemplate  "
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "e4a57303675ceabf"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "from langchain.prompts import PromptTemplate\n",
    "prompt = PromptTemplate.from_template(\"你是一个{name}, 帮我起一个具有{country}特色的男孩名字\")\n",
    "message = prompt.format(name=\"起名大师\", country=\"美国\")\n",
    "\n",
    "\n",
    "print(message)\n",
    "llm.predict(message)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "b8881a0bd7fbd8fb"
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 提示词模板- 对话模板 ChatPromptTemplate\n",
    "- 输入是一个数组"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "4eef73f9ae34ca12"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 对话模板具有结构，chatmodels\n",
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "\n",
    "chat_template = ChatPromptTemplate.from_messages(\n",
    "    [\n",
    "        (\"system\", \"你是一个起名大师. 你的名字叫{name}.\"),\n",
    "        (\"human\", \"你好{name},你感觉如何？\"),\n",
    "        (\"ai\", \"你好！我状态非常好!\"),\n",
    "        (\"human\", \"你叫什么名字呢?\"),\n",
    "        (\"ai\", \"你好！我叫{name}\"),\n",
    "        (\"human\", \"{user_input}\"),\n",
    "        # (\"human2\", \"{user_input}\"),\n",
    "    ]\n",
    ")\n",
    "\n",
    "chat_template.format_messages(name=\"陈大师\", user_input=\"你的爸爸是谁呢?\")\n"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "37bac1ba54a64d4b"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "\n",
    "from langchain.schema import SystemMessage\n",
    "from langchain.schema import HumanMessage\n",
    "from langchain.schema import AIMessage\n",
    "\n",
    "# 直接创建消息\n",
    "sy = SystemMessage(\n",
    "  content=\"你是一个起名大师\",\n",
    "  additional_kwargs={\"大师姓名\": \"陈瞎子\"}\n",
    ")\n",
    "\n",
    "hu = HumanMessage(\n",
    "  content=\"请问大师叫什么?\"\n",
    ")\n",
    "ai = AIMessage(\n",
    "  content=\"我叫陈瞎子\"\n",
    ")\n",
    "[sy,hu,ai]\n"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "ebecdc05215c8557"
  },
  {
   "cell_type": "markdown",
   "source": [
    "## ChatMessagePromptTemplate "
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "433d470103cdc53b"
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "data": {
      "text/plain": "ChatMessage(content='愿原力与你同在！', additional_kwargs={}, role='天行者')"
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.prompts import AIMessagePromptTemplate\n",
    "from langchain.prompts import SystemMessagePromptTemplate\n",
    "from langchain.prompts import HumanMessagePromptTemplate\n",
    "from langchain.prompts import ChatMessagePromptTemplate\n",
    "\n",
    "\n",
    "prompt = \"愿{subject}与你同在！\"\n",
    "chat_message_prompt = ChatMessagePromptTemplate.from_template(role=\"天行者\",template=prompt)\n",
    "chat_message_prompt.format(subject=\"原力\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-07T16:17:34.881409800Z",
     "start_time": "2024-05-07T16:17:34.847800400Z"
    }
   },
   "id": "b51e9191f376f875"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "fcea174793e42cf5"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
